package licode.unisop.client.feign.impl;

import licode.unisop.client.api.auth.AuthTokenShim;
import licode.unisop.client.api.auth.SopUserAuth;
import licode.unisop.client.feign.SopUserAuthFeign;
import licode.unisop.client.info.*;
import licode.unisop.client.utils.SopException;
import licode.unisop.client.utils.SopResult;
import licode.unisop.client.vo.SopAuthContext;
import licode.unisop.client.vo.SopAuthResult;
import licode.unisop.client.vo.SopAuthUser;
import licode.unisop.client.vo.SopCredential;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author licode
 */
@Service
public class SopUserAuthFeignImpl implements SopUserAuth {
    @Resource
    private AuthTokenShim authTokenShim;

    @Resource
    private SopUserAuthFeign sopUserAuthFeign;

    private String getModalToken(String platformId) {
        return authTokenShim.modalToken(platformId);
    }

    @Override
    public Map<String, Object> getCaptchaCode() {
        SopResult<Map<String, Object>> result;

        result = sopUserAuthFeign.getCaptchaCode();

        if (result.getCode() != 0) {
            throw new SopException(result.getCode(), result.getMessage());
        }

        return result.getData();
    }

    @Override
    public String updatePassword(SopUpdatePasswordIn info) {
        SopResult<String> result;

        result = sopUserAuthFeign.updatePassword(
                getModalToken(info.getAccessToken()),
                info,"",
                info.getAccessToken(),
                info.getPlatformId());

        if (result.getCode() != 0) {
            throw new SopException(result.getCode(), result.getMessage());
        }
        return result.getData();
    }

    @Override
    public SopAuthResult authorize(SopAuthIn accountReq) {
        SopResult<SopAuthResult> result;
        SopAuthContext context = accountReq.getContext();

        result = sopUserAuthFeign.authorize(
                getModalToken(context.getPlatformId()),
                context.getRealmId(),
                context.getTerminalCode(),
                context.getPlatformId(),
                context.getClientId(),
                context.getClientSecret(),
                accountReq.getGrantType(),
                accountReq.getScope(),
                accountReq.getCaptchaKey(),
                accountReq.getCaptchaCode(),
                accountReq);

        if (result.getCode() != 0) {
            throw new SopException(result.getCode(), result.getMessage());
        }

        return result.getData();
    }

    @Override
    public SopAuthResult authorizeDirect(SopAuthDirectIn accReq) {
        SopResult<SopAuthResult> result;
        SopAuthContext context = accReq.getContext();

        result = sopUserAuthFeign.authorizeDirect(
                getModalToken(context.getPlatformId()),
                context.getRealmId(),
                context.getPlatformId(),
                accReq);

        if (result.getCode() != 0) {
            throw new SopException(result.getCode(), result.getMessage());
        }

        return result.getData();
    }

    @Override
    public SopAuthUser token(SopCodeInfo authToken) {
        SopResult<SopAuthUser> result;

        result = sopUserAuthFeign.token(
                getModalToken(authToken.getPlatformId()),
                authToken.getTerminalId(),
                authToken.getPlatformId(),
                authToken.getCode(),
                authToken.getState(),
                authToken.getIpAddr(),
                authToken.getUserBrowser());

        if (result.getCode() != 0) {
            throw new SopException(result.getCode(), result.getMessage());
        }

        return result.getData();
    }

    @Override
    public SopAuthUser login(SopAuthIn accountReq) {
        SopResult<SopAuthUser> result;
        SopAuthContext context = accountReq.getContext();

        result = sopUserAuthFeign.login(
                getModalToken(context.getPlatformId()),
                context.getRealmId(),
                context.getTerminalCode(),
                context.getPlatformId(),
                context.getClientId(),
                context.getClientSecret(),
                accountReq.getGrantType(),
                accountReq.getScope(),
                accountReq.getCaptchaKey(),
                accountReq.getCaptchaCode(),
                accountReq.getIpAddr(),
                accountReq.getUserBrowser(),
                accountReq);

        if (result.getCode() != 0) {
            throw new SopException(result.getCode(), result.getMessage());
        }

        return result.getData();
    }

    @Override
    public SopAuthUser loginDirect(SopAuthDirectIn accReq) {
        SopResult<SopAuthUser> result;
        SopAuthContext context = accReq.getContext();

        result = sopUserAuthFeign.loginDirect(
                getModalToken(context.getPlatformId()),
                context.getRealmId(),
                context.getPlatformId(),
                accReq);

        if (result.getCode() != 0) {
            throw new SopException(result.getCode(), result.getMessage());
        }

        return result.getData();
    }

    @Override
    public SopCredential refresh(SopTokenInfo sopTokenInfo) {
        SopResult<SopCredential> result;

        result = sopUserAuthFeign.refresh(
                sopTokenInfo.getRealmId(),
                getModalToken(sopTokenInfo.getToken()),
                sopTokenInfo.getPlatformId(),
                sopTokenInfo.getToken());
        if (result.getCode() != 0) {
            throw new SopException(result.getCode(), result.getMessage());
        }
        return result.getData();
    }

    @Override
    public void logout(SopTokenInfo sopTokenInfo) {
        SopResult<Boolean> result;

        result = sopUserAuthFeign.logout(
                sopTokenInfo.getRealmId(),
                getModalToken(sopTokenInfo.getToken()),
                sopTokenInfo.getPlatformId(),
                sopTokenInfo.getToken());
        if (result.getCode() != 0) {
            throw new SopException(result.getCode(), result.getMessage());
        }
    }

    @Override
    public void logoutUser(SopUserIdIn req) {
        SopResult<Boolean> result;

        result = sopUserAuthFeign.logoutUser(req);
        if (result.getCode() != 0) {
            throw new SopException(result.getCode(), result.getMessage());
        }
    }

    @Override
    public void logoutSession(SopTokenInfo authToken) {
        SopResult<Boolean> result;

        result = sopUserAuthFeign.logoutSession(
                getModalToken(authToken.getToken()),
                authToken.getPlatformId(),
                authToken.getToken());
        if (result.getCode() != 0) {
            throw new SopException(result.getCode(), result.getMessage());
        }
    }
}
